home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / gfx / misc / Splitmpeg.lha / Splitmpeg / src / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-22  |  5.9 KB  |  242 lines

  1. /*
  2.  * Copyright (c) 1994 Michael Simmons.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  *
  10.  * IN NO EVENT SHALL MICHAEL SIMMONS BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF MICHAEL SIMMONS
  13.  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * THE MICHAEL SIMMONS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND MICHAEL SIMMONS HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  *
  21.  * I can be contacted via
  22.  * Email: michael@ecel.uwa.edu.au
  23.  * Post: P.O. Box 506, NEDLANDS WA 6009, AUSTRALIA
  24.  *
  25.  * Amigaversion by Tobias Seiler in 1997
  26.  * Email: tabs@blader.com
  27.  */
  28.  
  29. /*
  30.  * System layer spliter routines
  31.  */
  32.  
  33. #include    "main.h"
  34.  
  35. static Pack_Header     pack_header;
  36. static System_Header   system_header,system_header2;
  37. static Packet          packet;
  38.  
  39. /*
  40.  *
  41.  *
  42.  */
  43. void split_stream(char *szFileName)
  44. {
  45.   int BufLength;
  46.   unsigned int data;
  47.   int i, ret;
  48.   unsigned int err;
  49.  
  50.   if ((BitStream = fopen(szFileName,"rb")) == NULL){
  51.     errmsg(ERR_OPEN_BSTREAM);
  52.     return;
  53.   }
  54.  
  55.   /*    rewind(BitStream); */ /* Could be Socket ? later on */
  56.  
  57.   BufLength = ( BUF_LENGTH + 3) >> 2;
  58.   buf_start = (unsigned int *) malloc(BufLength * 4);
  59.   if( buf_start == NULL){
  60.     fclose(BitStream);
  61.     errmsg(ERR_MALLOC);
  62.     return;
  63.   }
  64.   max_buf_length = BufLength - 1;
  65.  
  66.   /* initialize bit io */
  67.   EOF_flag = 0;
  68.   bitOffset = 0;
  69.   bufLength = 0;
  70.   bitBuffer = buf_start;
  71.   curBits = *bitBuffer;
  72.  
  73.   /* initialize info on elementary streams */
  74.   for( i=0; i<MAX_NUM_STREAMS; i++){
  75.     streamInfo[i].stream = NULL;
  76.     streamInfo[i].STD_scale =0;
  77.     streamInfo[i].STD_size =0;
  78.     streamInfo[i].PTS_hibit = 0;
  79.     streamInfo[i].PTS = 0;
  80.     streamInfo[i].DTS_hibit = 0;
  81.     streamInfo[i].DTS = 0;
  82.   }
  83.  
  84.   ret = setjmp(env);      /* in case a fatal low level error occurs */
  85.   if (ret != 0 ) {
  86.     errmsg(ret);
  87.   }
  88.   else{
  89.     system_header_found = FALSE;
  90.     pack_cnt = packet_cnt = system_header_cnt = 0;
  91.  
  92.     next_start_code();
  93.     show_bits32(&data);
  94.     if( data != PACK_START_CODE){
  95.       errmsg(ERR_NOT_SYSTEM_LAYER);
  96.     }
  97.     else{
  98.       do{
  99.         if( err = parse_pack()){
  100.           errmsg(err);
  101.           break;
  102.         }
  103.         next_start_code();
  104.       }
  105.       while (next_bits(32,PACK_START_CODE));
  106.  
  107.       if( (err == NO_ERROR) && !next_bits(32,ISO_11172_END_CODE))
  108.       errmsg(ERR_MISSING_END_CODE);
  109.     }
  110.   }
  111.  
  112.   dialog_end();
  113.  
  114.   for( i=0; i<MAX_NUM_STREAMS; i++)
  115.   if( streamInfo[i].stream != NULL ){
  116.     fclose(streamInfo[i].stream);
  117.     streamInfo[i].stream = NULL;
  118.   }
  119.  
  120.   if( BitStream != NULL){
  121.     fclose(BitStream);
  122.     BitStream = NULL;
  123.   }
  124. }
  125.  
  126.  
  127. /*
  128.  * Parse a system layer pack
  129.  * each packet extracted is pass to process_packet for processing
  130.  */
  131.  
  132. int parse_pack()
  133. {
  134.   unsigned int data;
  135.   int err;
  136.  
  137.   if( err = parse_pack_header(&pack_header) )
  138.   return (err | ERR_PARSE_PACK);
  139.  
  140.   pack_cnt++;
  141.   if( verbose_flag && !quiet_flag )
  142.   dialog_pack_header(&pack_header , pack_cnt);
  143.  
  144.   if( next_bits(32,SYSTEM_HEADER_START_CODE)){
  145.     system_header_cnt++;
  146.     if( system_header_found ){
  147.       if( err = parse_system_header( &system_header2 ) )
  148.       return (err | ERR_PARSE_PACK);
  149.       if( err = compare_system_headers(&system_header,&system_header2) )
  150.       return (err | ERR_PARSE_PACK);
  151.     }
  152.     else{
  153.       system_header_found = TRUE;
  154.       if( err = parse_system_header( &system_header ))
  155.       return (err | ERR_PARSE_PACK);
  156.       dialog_system_header(&system_header);
  157.     }
  158.   }
  159.  
  160.   show_bits32(&data);
  161.   while( (RESERVED_STREAM <= data) && (data <= RESERVED_DATA_STREAM_15) ){
  162.     if( system_header_found == FALSE )
  163.     return (ERR_PARSE_PACK | ERR_MISSING_SYSTEM_HDR);
  164.     if( err = parse_packet(&packet) ){
  165.       cleanup_packet(&packet);
  166.       return (err | ERR_PARSE_PACK);
  167.     }
  168.     if( err = process_packet(&packet) ){
  169.       cleanup_packet(&packet);
  170.       return (err | ERR_PARSE_PACK);
  171.     }
  172.     cleanup_packet(&packet);
  173.     show_bits32(&data);
  174.   }
  175.  
  176.   return NO_ERROR;
  177. }
  178.  
  179. /*
  180.  * Processes a packet of stream data
  181.  * At the moment it appends the packet to a file
  182.  * created for each elemental stream.
  183.  */
  184. int process_packet(Packet *packet)
  185. {
  186.   unsigned int byteswritten;
  187.   unsigned int stream_num;
  188.  
  189.   packet_cnt++;
  190.  
  191.   stream_num = packet->stream_id - (RESERVED_STREAM & 0xff);
  192.  
  193.   if ( streamInfo[stream_num].stream == NULL ){
  194.         char szBuf[32];
  195.     sprintf(szBuf,"stm%d.mpg\0",packet->stream_id);
  196.     streamInfo[stream_num].stream = fopen(szBuf,"w");
  197.     if( streamInfo[stream_num].stream == NULL ){
  198.       return (ERR_OPEN_ESTREAM | ERR_PROCESS_PACKET);
  199.     }
  200.     if( verbose_flag && !quiet_flag ){
  201.       char szMsgBuf[100];
  202.       sprintf(szMsgBuf,"Opening stream file %s",szBuf);
  203.       dialog_msg(szMsgBuf);
  204.     }
  205.   }
  206.  
  207.   if( packet->buffer == NULL )
  208.   return (ERR_NO_PACKET_BUFFER | ERR_PROCESS_PACKET);
  209.  
  210.   byteswritten = fwrite( (char *)packet->buffer, 1, packet->buffer_size, streamInfo[stream_num].stream);
  211.  
  212.   if( byteswritten != packet->buffer_size )
  213.   return (ERR_WRITE_ESTREAM | ERR_PROCESS_PACKET);
  214.  
  215.   return NO_ERROR;
  216. }
  217.  
  218. /*
  219.  * Cleans up a packet after it has been processed
  220.  */
  221. void cleanup_packet(Packet *packet)
  222. {
  223.   if( packet->buffer != NULL){
  224.     free(packet->buffer);
  225.     packet->buffer = NULL;
  226.   }
  227. }
  228.  
  229. /*
  230.  * This routine compares two system headers and returns and error
  231.  * if they are different
  232.  * Only a stub at the moment
  233.  */
  234. int compare_system_headers(System_Header *system_header,System_Header *system_header2)
  235. {
  236.   if (FALSE )
  237.   return ERR_DIFF_SYSTEM_HDR;
  238.  
  239.   return NO_ERROR;
  240. }
  241.  
  242.